<template>
  <div id="app-container">
    <u-page title="商友临时扣款单">
      <el-descriptions class="frominfo" :model="form">
        <el-descriptions-item label="申请人员">{{ form.applicantnickname }}【{{ form.applicantusername }}】</el-descriptions-item>
        <el-descriptions-item label="表单编号">{{ form.code }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ form.applyTime }}</el-descriptions-item>
        <el-descriptions-item label="填表人员">{{ form.preparernickname }}【{{ form.preparerusername }}】</el-descriptions-item>
        <el-descriptions-item label="表单状态">{{ form.statusstr }}</el-descriptions-item>
        <el-descriptions-item label="结束时间">{{ form.endTime }}</el-descriptions-item>
        <!--        <el-descriptions-item label="当前处理人"></el-descriptions-item>-->
      </el-descriptions>
      <el-form ref="contractform" :model="form" :rules="contractform" label-width="110px" label-position="left" size="mini">
        <div class="contractDetailBox">
          <el-row>
            <el-col :span="6">
              <el-form-item label="扣费部门" prop="deptid" style="padding-top: 10px">
                <el-cascader
                  ref="depts"
                  v-model="form.deptid"
                  :options="deptList"
                  :show-all-levels="false"
                  :disabled="cansave||form.demanddetailsData.length!=0"
                  @change="handledeptChanged"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="扣款项目" prop="deductname" style="padding-top: 10px">
                <el-cascader
                  ref="deductnames"
                  v-model="form.deductname"
                  :options="deductNames"
                  :show-all-levels="false"
                  :disabled="cansave||form.demanddetailsData.length!=0"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="缴款时间" prop="signDate">
            <el-date-picker
              v-model="form.signDate"
              type="month"
              placeholder="选择年月"
              :disabled="cansave"
              @change="disabledDate"
            />
          </el-form-item>
          <el-row v-if="form.deptid.substring(0,4)==='0207'">
            <el-col :span="6">
              <el-form-item label="合同部门" prop="htdeptid" style="padding-top: 10px">
                <el-cascader
                  ref="htdepts"
                  v-model="form.htdeptid"
                  :options="deptList"
                  :show-all-levels="false"
                  @change="handlehtdeptChanged"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="需求详情" />
          <div>
            <template>
              <el-table ref="detailfrom" :data="form.demanddetailsData" :border="true">
                <el-table-column label="序号" width="60">
                  <template slot-scope="scope">
                    {{ scope.$index + 1 }}
                  </template>
                </el-table-column>
                <el-table-column key="choose" label="选择合同" prop="choose" width="140">
                  <template slot-scope="scope">
                    <el-button type="text" @click="fetchData(scope.$index)">选择合同</el-button>
                  </template>
                </el-table-column>
                <el-table-column key="firmcode" label="厂商编号" prop="firmcode" width="180">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.firmcode" style="width: 155px" :disabled="true" />
                  </template>
                </el-table-column>
                <el-table-column key="firmname" label="厂商名称" prop="firmname" width="280">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.firmname" style="width: 250px" :disabled="true" />
                  </template>
                </el-table-column>
                <el-table-column key="hth" label="合同号" prop="hth" width="180">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.hth" style="width: 155px" :disabled="true" />
                  </template>
                </el-table-column>
                <el-table-column key="zgid" label="专柜编号" prop="zgid" width="180">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.zgid" style="width: 155px" :disabled="true" />
                  </template>
                </el-table-column>
                <el-table-column key="zgname" label="专柜名称" prop="zgname" width="180">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.zgname" style="width: 155px" :disabled="true" />
                  </template>
                </el-table-column>
                <el-table-column key="kktype" label="扣款类型" prop="kktype" width="160">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.kktype" placeholder="请选择扣款类型" :disabled="cansave" width="140">
                      <el-option v-for="item in kktypes" :label="item.label" :value="item.value" />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column key="amount" label="单价" prop="amount" width="150">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.amount" :disabled="cansave" @change="handleProcesscountChanged(scope.row)" @input="limitToTwoDecimals(scope.row)" />
                  </template>
                </el-table-column>
                <el-table-column key="processcount" label="数量" prop="processcount" width="150">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.processcount" style="width: 100px" :disabled="cansave" @input="limitToSixDecimals(scope.row)" @change="handleProcesscountChanged(scope.row)" />
                  </template>
                </el-table-column>
                <el-table-column key="totalmoney" label="合计金额" prop="totalmoney" width="150">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.totalmoney" placeholder="" type="" :disabled="true" />
                  </template>
                </el-table-column>
                <el-table-column key="bz" label="备注" prop="bz" width="150">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.bz" placeholder="请填写备注" :disabled="cansave" />
                  </template>
                </el-table-column>
                <el-table-column key="status" label="状态" prop="status" width="150">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.status" placeholder="" :disabled="true" />
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="auto">
                  <template slot-scope="scope">
                    <el-button size="mini" type="danger" plain :disabled="cansave" @click="deldetails(scope.$index)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div style="display: flex; align-items: center;margin-top: 5px">
                <el-button class="el-icon-circle-plus-outline" type="primary" style="margin-right: 10px;" :disabled="cansave" @click="adddetails" />
                <!--                <el-button :disabled="cansave" @click="addalldetails" >批量导入</el-button>-->
                <u-upload-attachment v-if="form.deptid!==''&&form.deductname!==''" v-model="form.attachmentList" name="file" :size="10" accept=".xlsx,.xls,.csv" :multiple="true" :disabled="cansave" @change="addalldetails">批量导入</u-upload-attachment>
                <el-link type="primary" style="padding-left: 15px" @click="downloadAttachment">下载模版</el-link>
              </div>
            </template>
          </div>
          <el-row>
            <el-col :span="8">
              <el-form-item label="扣费合计" prop="summoney">
                <u>{{ form.summoney }}</u>元
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div style="margin-top: 5px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="附件" prop="contractUrl">
                <u-upload-multiple-files v-model="form.fileList" name="file" :disabled="cansave" :size="10" :limit="2" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="扣款单号" prop="deductioncode" :hidden="form.deductioncode===0">
                <span>{{ form.deductioncode }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="收入缴存单号" prop="paymentcode" :hidden="form.paymentcode===''">
                <span>{{ form.paymentcode }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="说明" prop="remarks">
              <textarea v-model="form.remarks" style="width:80%;height: 150px;" :disabled="cansave" />
            </el-form-item>
          </el-row>
        </div>
        <el-form-item v-if="form.applicantusername===form.viewerusername">
          <el-button type="primary" :disabled="cansave" @click="onSave('contractform')">保存</el-button>
          <el-button type="primary" :disabled="cansubmit" @click="onSubmit()">发起流程</el-button>
          <el-button type="primary" :disabled="canmodify" @click="onModify()">修改</el-button>
          <el-button :disabled="canrevoke" @click="onRevoke()">撤销流程</el-button>
          <el-button :disabled="disableflag" @click="onAbate('contractform')">作废</el-button>
        </el-form-item>
      </el-form>
    </u-page>
    <el-drawer
      title="厂商合同信息"
      :visible.sync="fcInfotable"
      direction="rtl"
      size="70%"
    >
      <div style="margin-top: 5px;padding-left: 10px">
        <el-row>
          <el-col :span="8">
            <el-input v-model="keyWord" placeholder="请输入内容" class="input-with-select" @input="keyWordChange()" />
          </el-col>
          <el-col :span="12">
            <div style="font-size:12px;padding-top: 7px">（可输入厂商编号、或厂商名称、或专柜编号、或专柜名称的部份文字进行筛选）</div>
          </el-col>
        </el-row>
      </div>
      <u-table
        :data="fcInfoData"
        :options="listOptions"
        :columns="fcInfocolumns"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="getFirm(row)">选择</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>
    </el-drawer>
  </div>
</template>

<script>

import { mapGetters } from 'vuex'

export default {
  components: {
  },
  data() {
    return {
      api: this.$u.api.ContractApply,
      dataapi: this.$u.api.ContractFormdata,
      attachmenturl: 'https://csms.chiconysquare.net/assets/contract/扣款单导入模版.xls',
      listOptions: {
        stripe: true
      },
      dates: 0,
      selectIndex: 0,
      // 保存按钮激活
      cansave: false,
      // 提交按钮激活
      cansubmit: true,
      // 修改按钮激活
      canmodify: true,
      // 撤销流程按钮
      canrevoke: true,
      // 作废按钮
      disableflag: true,
      // 合同类型数组
      kktypes: [{ label: '账扣', value: '账扣' }, { label: '现扣', value: '现扣' }],
      formname: '流水号',
      // 合同数据
      contractlist: [],
      // 楼层
      floors: [],
      // 总金额
      sum: 0,
      deptinfo: {},
      deptList: [],
      deductNames: [],
      // 表单所有数据
      form: {
        code: 0,
        updateflag: false,
        typename: '商友临时扣款单',
        statusstr: '',
        // 人员信息
        applicantnickname: '',
        applicantusername: '',
        applicantuserid: '',
        preparernickname: '',
        preparerusername: '',
        prepareruserid: '',
        viewerusername: '',
        viewernickname: '',
        applyTime: '',
        endTime: '',
        // usertype: '',
        dept: '',
        deptname: '',
        deptid: '',
        deductname: '',
        summoney: 0,
        remarks: '',
        demanddetailsData: [],
        attachmentList: [],
        // 联系人资料
        signDate: '',
        yearmonth: '',
        instanceId: '',
        contractname: '',
        deductioncode: 0,
        paymentcode: '',
        contractpath: '',
        htdeptid: ''
      },
      // 规则
      contractform: {
        deptid: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        deductname: [
          { required: true, message: '请选择扣费项目', trigger: 'change' }
        ],
        signDate: [
          { required: true, message: '请选择缴款日期', trigger: 'change' }
        ],
        summoney: [
          { required: true, message: '请填写合计金额', trigger: 'blur' }
        ]
      },
      value: '',
      fcInfotable: false,
      // 厂商合同信息
      fcInfoData: [],
      searchfcInfoData: [],
      keyWord: '',
      searchWord: '',
      fcInfocolumns: [
        {
          prop: 'firmcode',
          label: '厂商编号'
        },
        {
          prop: 'firmname',
          label: '厂商名称'
        },
        {
          prop: 'contractcode',
          label: '合同编号'
        },
        {
          prop: 'startdate',
          label: '开始日期',
          render: (h, params) => {
            return h('el-tag', this.$u.timeFormat(new Date(params.row.startdate).getTime(), 'yyyy-mm-dd'))
          }
        },
        {
          prop: 'enddate',
          label: '结束日期',
          render: (h, params) => {
            return h('el-tag', this.$u.timeFormat(new Date(params.row.enddate).getTime(), 'yyyy-mm-dd'))
          }
        },
        {
          prop: 'status',
          label: '状态',
          render: (h, params) => {
            if (params.row.status === '正常') {
              return h('el-tag', { props: { type: 'success' }}, '正常')
            } else if (params.row.status === '到期') {
              return h('el-tag', { props: { type: 'danger' }}, '到期')
            } else {
              return h('el-tag', params.row.status)
            }
          }
        },
        {
          prop: 'deptid',
          label: '专柜编号'
        },
        {
          prop: 'dept_name',
          label: '专柜名称'
        },
        {
          prop: 'spsb',
          label: '商标'
        }
      ]
    }
  },
  created() { },
  mounted() {
    this.getDeptList()
    this.getDeductList()
    // 如果传了参数，则加载数据
    if (this.$route.query.formcode != null) {
      this.getViewers()
      this.getSyDeductFormdata(this.$route.query.formcode)
    } else {
      this.getApplicant()
    }
  },
  computed: {
    ...mapGetters([
      'nickname'
    ])
  },
  methods: {
    // 只可选择当前或之后月份
    disabledDate() {
      const self = this
      const today = new Date()
      // 获取当前年月
      const currentYear = today.getFullYear()
      const currentMonth = today.getMonth()// 月份从0开始，所以1月是0，12月是11

      // 判断选中的月份是否早于当前月份
      if (self.form.signDate.getFullYear() < currentYear || (self.form.signDate.getFullYear() === currentYear && self.form.signDate.getMonth() < currentMonth)) {
        // 提示信息
        this.$message.error('日期不能小于当前月份')
        self.form.signDate = '' // 清空选中的日期
        return // 发现空字段，返回
      }
      const year = self.form.signDate.getFullYear()
      const month = String(self.form.signDate.getMonth() + 1).padStart(2, '0') // 月份从0开始，需要加1并补零
      this.form.signDate = `${year}-${month}-01`
    },
    // 加载申请人员
    getApplicant() {
      const self = this
      self.api.applicant().then(res => {
        self.form.applicantusername = res.data.username
        self.form.applicantnickname = res.data.usernickname
        self.form.applicantuserid = res.data.userid
        self.form.preparerusername = res.data.username
        self.form.preparernickname = res.data.usernickname
        self.form.prepareruserid = res.data.userid
        self.form.viewerusername = res.data.username
        self.form.viewernickname = res.data.usernickname
        self.form.applyTime = res.data.nowtime
      })
    },
    // 加载查看人
    getViewers() {
      const self = this
      self.api.applicant().then(res => {
        self.form.viewerusername = res.data.username
        self.form.viewernickname = res.data.usernickname
      })
    },
    // 加载部门
    getDeptList() {
      const self = this
      self.api.deptList().then(res => {
        this.deptList = res.data
      })
    },
    // 加载扣款项目
    getDeductList() {
      const self = this
      self.api.deductnames().then(res => {
        this.deductNames = res.data
      })
    },
    // 保存
    onSave(formName) {
      const self = this
      self.$confirm('确认保存吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 表单验证
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 子表单判断
            if (self.form.demanddetailsData.length === 0) {
              self.$message.error('保存失败！需求详情错误！')
              return
            }
            // 判断填写内容完整性
            for (let i = 0; i < this.form.demanddetailsData.length; i++) {
              const row = this.form.demanddetailsData[i]
              // 检查必要字段是否为空
              if (!row.firmcode || !row.hth || !row.kktype || !row.amount || row.processcount === null) {
                this.$message.error(`第 ${i + 1} 行填写不完整，请检查所有必填项。`)
                return // 发现空字段，返回
              }
              if (!(row.status === '正常' || row.status === '到期')) {
                this.$message.error(`第 ${i + 1} 行合同状态无法申请扣款，请检查合同号是否正确。`)
                return
              }
              if (self.form.deptid.substring(0, 4) !== '0207') {
                if (!row.zgid.startsWith(self.form.deptid)) {
                  this.$message.error(`第 ${i + 1} 行专柜不属于扣款部门，请检查合同号是否正确。`)
                  return
                }
              }
            }
            // 通过验证进行提交动作
            self.api.save(self.form).then(res => {
              // 更新表单编号
              this.form.code = res.data
              // 将表单设置为只读
              this.cansave = true
              // 修改提交作废按钮变亮
              this.cansubmit = false
              this.canmodify = false
              this.canrevoke = true
              this.disableflag = false
              // 通知信息
              self.$u.msg(res.msg)
            }).catch(() => {
              return
            })
          } else {
            return false
          }
        })
      }).catch(() => {
        return
      })
    },
    // 修改
    onModify() {
      this.cansave = false
      this.cansubmit = true
      this.canmodify = true
      this.form.updateflag = true
    },
    // 提交
    onSubmit() {
      const self = this
      // 通过验证进行提交动作
      self.api.submit(self.form).then(res => {
        // 通知信息
        self.$u.msg(res.msg)
        // 初始化数据
        // 刷新页面
        self.$router.go(0)
      }).catch(() => {
        return
      })
    },
    // 撤销钉钉流程
    onRevoke() {
      const self = this
      // 通过验证进行提交动作
      self.api.revoke(self.form).then(res => {
        // 通知信息
        self.$u.msg(res.data)
        // 修改按钮状态
        // 将表单设置为只读
        this.cansave = true
        this.cansubmit = true
        this.canmodify = false
        this.canrevoke = true
        this.disableflag = false
        self.form.statusstr = '撤销流程'
      }).catch(() => {
        return
      })
    },
    // 作废
    onAbate() {
      const self = this
      // 通过验证进行提交动作
      self.api.abate(self.form).then(res => {
        // 通知信息
        self.$u.msg(res.data)
        // 刷新页面
        self.$router.go(0)
      }).catch(() => {
        return
      })
    },
    // 编辑功能:监听组织变化
    handledeptChanged(value, label) {
      const self = this
      self.form.deptname = this.$refs['depts'].getCheckedNodes()[0].label
      self.form.deptid = this.$refs['depts'].getCheckedNodes()[0].value
    },
    handlehtdeptChanged(value, label) {
      const self = this
      self.form.htdeptid = this.$refs['htdepts'].getCheckedNodes()[0].value
    },
    // 添加需求详情
    adddetails() {
      const self = this
      if (self.form.deptname === '') {
        self.$message.error('请先选择扣款部门！')
        return
      }
      if (self.form.deductname === '') {
        self.$message.error('请先选择扣款项目！')
        return
      }
      self.form.demanddetailsData.push({
        kktype: '',
        amount: '',
        status: '',
        bz: '',
        processcount: 0,
        totalmoney: 0,
        ifconsistent: 0
      })
    },
    // 批量导入数据
    addalldetails() {
      const self = this
      self.api.addalldetails(self.form).then(res => {
        self.form.demanddetailsData = res.data
        // 重新计算合计金额
        this.handlemoneyChanged()
      })
    },
    // 删除需求详情
    deldetails(index) {
      const self = this
      self.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确定的操作
        self.form.demanddetailsData.splice(index, 1)
        // 重新计算合计金额
        this.handlemoneyChanged()
      }).catch(() => {
        return
      })
    },

    handleProcesscountChanged(row) {
      if (row.amount !== '') {
        // row.totalmoney = parseFloat(row.amount) * row.processcount
        const sumprev = (Math.round(row.amount * 100) * Math.round(row.processcount * 1000000)) / 100000000
        row.totalmoney = sumprev.toFixed(2)
        this.handlemoneyChanged()
      }
    },
    // 金额改变
    handlemoneyChanged() {
      const self = this
      self.sum = 0
      self.form.demanddetailsData.forEach(item => {
        self.sum += Math.round(item.totalmoney * 100)
      })
      self.sum = self.sum / 100
      self.form.summoney = self.sum.toFixed(2)
    },
    // 选择厂商合同数据
    fetchData(index) {
      const self = this
      self.selectIndex = index
      self.api.allfcInfos(self.form).then(res => {
        this.fcInfoData = res.data
        this.searchfcInfoData = res.data
      })
      self.fcInfotable = true
    },
    // 输入关键字
    keyWordChange() {
      const self = this
      // 表格用原表格的数据 即 用于搜索的总数据
      // self.fcInfoData = self.searchfcInfoData
      const _search = self.keyWord
      const newListData = [] // 用于存放搜索出来数据的新数组
      if (self.keyWord !== '') {
        // filter 过滤数组
        this.searchfcInfoData.filter((item) => {
          // newListData中 没有查询的内容，就添加到newListData中
          if (
            item.firmname.indexOf(_search) !== -1 ||
            item.firmcode.indexOf(_search) !== -1 ||
            item.deptid.indexOf(_search) !== -1 ||
            item.dept_name.indexOf(_search) !== -1
          ) {
            newListData.push(item)
          }
        })
        this.fcInfoData = newListData
      } else {
        self.fcInfoData = self.searchfcInfoData
      }
    },
    //  获取表单数据并初始化参数
    getSyDeductFormdata(formcode) {
      const self = this
      self.form.code = formcode
      self.form.typename = '商友临时扣款单'
      self.api.sydeductformdata(self.form).then(res => {
        const maindata = res.data
        const { syDeductdata } = maindata
        self.form.applicantusername = maindata.applicantusername
        self.form.applicantnickname = maindata.applicantnickname
        self.form.applicantuserid = maindata.applicantuserId
        self.form.preparerusername = maindata.preparerusername
        self.form.preparernickname = maindata.preparernickname
        self.form.applyTime = maindata.starttime
        self.form.endTime = maindata.endtime
        if (maindata.instanceId !== '') {
          self.form.instanceId = maindata.instanceId
        }
        if (syDeductdata.deductioncode !== 0) {
          self.form.deductioncode = syDeductdata.deductioncode
        }
        self.form.deductname = syDeductdata.deductname
        self.form.deptname = syDeductdata.deptname
        self.form.deptid = syDeductdata.deptid
        self.form.summoney = syDeductdata.summoney
        self.form.remarks = syDeductdata.remarks
        self.form.fileList = syDeductdata.fileList
        self.form.signDate = syDeductdata.signDateStr
        self.form.dept = syDeductdata.dept
        // sydeductdata
        if (maindata.status === 0) {
          // 将表单设置为只读
          this.cansave = true
          this.cansubmit = false
          this.canmodify = false
          this.canrevoke = true
          this.disableflag = false
          self.form.statusstr = '未提交'
        } else if (maindata.status === 1) {
          this.cansave = true
          this.cansubmit = true
          this.canmodify = true
          this.canrevoke = false
          this.disableflag = false
          self.form.statusstr = '流转中'
        } else if (maindata.status === 2) {
          this.cansave = true
          this.cansubmit = true
          this.canmodify = true
          this.canrevoke = true
          this.disableflag = true
          self.form.statusstr = '签核完成'
        } else if (maindata.status === 3) {
          // 将表单设置为只读
          this.cansave = true
          this.cansubmit = true
          this.canmodify = false
          this.canrevoke = true
          this.disableflag = false
          self.form.statusstr = '驳回'
        } else if (maindata.status === 4) {
          // 将表单设置为只读
          this.cansave = true
          this.cansubmit = true
          this.canmodify = false
          this.canrevoke = true
          this.disableflag = false
          self.form.statusstr = '撤销流程'
        } else if (maindata.status === 5) {
          this.cansave = true
          this.canmodify = true
          this.cansubmit = true
          this.canrevoke = true
          this.disableflag = true
          self.form.statusstr = '作废'
        }
        self.form.demanddetailsData = syDeductdata.demanddetailsData
      })
    },
    // 厂商弹窗选择功能
    getFirm(row) {
      const self = this
      const index = self.selectIndex
      /* self.form.demanddetailsData[index].firmcode = row.firmcode
      self.form.demanddetailsData[index].firmname = row.firmname
      self.form.demanddetailsData[index].hth = row.contractcode
      self.form.demanddetailsData[index].zgid = row.deptid
      self.form.demanddetailsData[index].zgname = row.dept_name*/
      // 使用 this.$set 来确保响应式更新
      self.$set(self.form.demanddetailsData, index, {
        ...self.form.demanddetailsData[index], // 保留原有字段
        firmcode: row.firmcode,
        firmname: row.firmname,
        hth: row.contractcode,
        zgid: row.deptid,
        zgname: row.dept_name,
        status: row.status
      })
      self.fcInfotable = false
    },
    // 下载合同文档
    downloadAttachment() {
      const self = this
      const a = document.createElement('a')
      a.href = self.attachmenturl
      a.download = '扣款单导入模版.xls'// 设置下载文件的名称
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
    },
    // 限制两位小数
    limitToTwoDecimals(row) {
      let value = row.amount

      // 允许负数和小数
      const reg = /^-?\d+(\.\d{0,2})?$/

      // 如果输入不符合要求，进行处理
      if (!reg.test(value)) {
        // 只保留数字、负号和小数点
        value = value.replace(/[^\d.-]/g, '') // 只保留数字、负号和小数点
          .replace(/--/g, '-') // 处理连续负号
          .replace(/^(-?\d+)\.(\d{2}).*$/, '$1.$2') // 保留小数点后两位
      }
      // 限制小数点后最多两位
      const parts = value.split('.')
      if (parts.length > 2) {
        value = parts[0] + '.' + parts[1] // 只保留小数点前和第一个小数部分
      } else if (parts.length === 2 && parts[1].length > 2) {
        value = parts[0] + '.' + parts[1].slice(0, 2)// 限制小数点后两个数字
      }

      // 更新 row.amount
      row.amount = value
    },
    // 输入分解数字限制小数
    limitToSixDecimals(row) {
      let value = row.processcount

      // 允许负数和小数
      const reg = /^-?\d+(\.\d{0,6})?$/

      // 如果输入不符合要求，进行处理
      if (!reg.test(value)) {
        // 只保留数字、负号和小数点
        value = value.replace(/[^\d.-]/g, '') // 只保留数字、负号和小数点
          .replace(/--/g, '-') // 处理连续负号
          .replace(/^(-?\d+)\.(\d{6}).*$/, '$1.$2') // 保留小数点后六位
      }

      // 限制小数点后最多六位
      const parts = value.split('.')
      if (parts.length > 2) {
        value = parts[0] + '.' + parts[1] // 只保留小数点前和第一个小数部分
      } else if (parts.length === 2 && parts[1].length > 6) {
        value = parts[0] + '.' + parts[1].slice(0, 6) // 限制小数点后六个数字
      }
      // 更新 row.count
      row.processcount = value
    }
  }
}
</script>

<style scoped>
.contractCheckBox {
  padding-bottom: 5px;
  border-bottom: 1px solid #c0c4cc;
}
.contractCheckBox .el-form-item{
  margin-bottom:3px;
}
.contractCheckBox .el-form-item--small .el-form-item{
  margin-bottom:3px;
}
.contractDetailBox{
  padding-top: 5px;
  border-bottom: 1px solid #c0c4cc;
}
.ContractLinkBox{
  padding-top: 10px;
}
::v-deep input[disabled],input:disabled,input.disabled {
  color:#000000 !important;
  background-color:#f1f1f1 !important;
}
::v-deep textarea[disabled],textarea:disabled,textarea.disabled {
  color:#000000 !important;
  background-color:#f1f1f1 !important;
}
</style>
